<template>
  <div class="shop-confirm" :class="{fade}">
    <div class="wrapper" :class="{fade}">
      <div class="header">
        <h3>{{title}}</h3>
        <a @click="cancel" href="JavaScript:;" class="iconfont icon-close-new"></a>
      </div>
      <div class="body">
        <i class="iconfont icon-warning"></i>
        <span>{{text}}</span>
      </div>
      <div class="footer">
        <ShopButton @click="cancel" size="mini" type="gray">取消</ShopButton>
        <ShopButton @click="confirm" size="mini" type="primary">确认</ShopButton>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, ref } from 'vue-demi'
import ShopButton from './shop-button.vue'
export default {
  name: 'ShopConfirm',
  components: { ShopButton },
  props: {
    // 对话框标题
    title: {
      type: String,
      default: '温馨提示'
    },
    // 提示文字
    text: {
      type: String,
      default: ''
    },
    // 点击取消触发的函数
    cancelCallback: {
      type: Function
    },
    // 点击确认触发的函数
    confirmCallback: {
      type: Function
    }
  },
  setup (props) {
    // 对话框默认隐藏
    const fade = ref(false)
    // 组件渲染完成后
    onMounted(() => {
      // 过渡效果需要在元素创建完毕后延时一会才会触发
      setTimeout(() => {
        fade.value = true
      }, 0);
    })
    // 点击取消关闭对话框
    const cancel = () => {
      props.cancelCallback()
    }
    // 点击确认关闭对话框
    const confirm = () => {
      props.confirmCallback()
    }
    return { fade, cancel, confirm }
  }
}
</script>

<style scoped lang="less">
.shop-confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0,0,0,0);
  &.fade {
    transition: all 0.4s;
    background: rgba(0,0,0,.5);
  }
  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-60%);
    opacity: 0;
    &.fade {
      transition: all 0.4s;
      transform: translate(-50%,-50%);
      opacity: 1;
    }
    .header,.footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
      .shop-button {
        margin-left: 15px;
      }
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: right;
      .Shop-button {
        margin-left: 20px;
      }
    }
    .header {
      position: relative;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
